package com.company.practice;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import lombok.SneakyThrows;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Random;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @Autowired的简单实现
 */

public class Test67 {



    public static void main(String[] args) {

        MyController myController = new MyController();
        Class<? extends MyController> aClass = myController.getClass();
        Field[] fields = aClass.getFields();
        Stream.of(fields).forEach(new Consumer<Field>() {
            @SneakyThrows
            @Override
            public void accept(Field field) {
                Annotation[] annotations = field.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType() == MyAutowired.class) {
                        System.out.println("有MyAutowired注解");
                        field.setAccessible(true);
                        //Class<? extends Annotation> aClass1 = annotation.getClass();
                        Class<?> type = field.getType();
                        Object o = null;
                        try {
                            o = type.newInstance();
                            field.set(myController, o);
                        } catch (InstantiationException e) {
                            e.printStackTrace();

                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }


                    }

                }

            }
        });


        System.out.println(myController.getMyService());
    }


}


